﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Setup
{
    [ApiExplorerSettings(GroupName = "SD")]
    public class RoleAppService : ApplicationService, IRoleAppService
    {
        private readonly IRepository<RoleManage, int> _RoleRepository;
        private readonly LogisticsDbContext _db;

        public RoleAppService(IRepository<RoleManage, int> RoleRepository, LogisticsDbContext db)
        {
            _RoleRepository = RoleRepository;
            _db = db;
        }
        /// <summary>
        /// 角色显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<RoleDto>> GetRoleAsync(GetRoleListDto dto)
        {

            //上下文对象取值
            var rolelist = _db.RoleManage.ToList();
            //判断是否为空
            if (!string.IsNullOrEmpty(dto.Name))
            {
                //添加查询
                rolelist = rolelist.Where(m => m.Name.Contains(dto.Name)).ToList();
            }
            //创建分页获取数据对象
            PagedResultDto<RoleDto> page = new PagedResultDto<RoleDto>();
            //总条数赋值
            page.TotalCount = rolelist.Count;

            //分页
            rolelist = rolelist.Skip(dto.SkipCount).Take(dto.MaxResultCount).ToList();

            //数据赋值
            page.Items = ObjectMapper.Map<List<RoleManage>, List<RoleDto>>(rolelist);
            //返回对象
            return page;
        }

        /// <summary>
        /// 获取权限
        /// </summary>
        /// <returns></returns>
        public List<JurisdictionDto> GetJurisdictions()
        {
            var rolelist = _db.PowerManager.ToList();
            var p = ObjectMapper.Map<List<PowerManager>, List<JurisdictionDto>>(rolelist);
            return p;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<RoleDto> CreateAddAsync(CreateRoleDto input)
        {
            var studentDto = new RoleManage
            {
                Name = input.Name,
                Pid = input.Pid,
                RoleAlias = input.RoleAlias,
                CreateTime = DateTime.Now,
            };
            await _RoleRepository.InsertAsync(studentDto);
            return ObjectMapper.Map<RoleManage, RoleDto>(studentDto);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _RoleRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="studentDto"></param>
        /// <returns></returns>
        public async Task<RoleDto> GetAsync(int id)
        {
            var student = await _RoleRepository.GetAsync(id);
            return ObjectMapper.Map<RoleManage, RoleDto>(student);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task UpdateAsync(int id, UpdateRoleDto input)
        {
            var receipts = await _RoleRepository.GetAsync(id);

            receipts.Name = input.Name;
            receipts.Pid = input.Pid;
            receipts.RoleAlias = input.RoleAlias;
            receipts.CreateTime = DateTime.Now;

            await _RoleRepository.UpdateAsync(receipts);

        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeletePdeleteAsync(string ids)
        {
            var a = ids.Split(',');
            foreach (var item in a)
            {
                var list = _db.RoleManage.Find(Convert.ToInt32(item));
                _db.RoleManage.RemoveRange(list);
            }

            var i = _db.SaveChanges();
            return i;
        }
        /// <summary>
        /// 角色下拉框
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<rxlDto> GetRoleslx()
        {
            var yg = _db.RoleManage.ToList();
            var p = ObjectMapper.Map<List<RoleManage>, List<rxlDto>>(yg);
            return p;
        }

        /// <summary>
        /// 获取角色权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<RoleLinkPowerDto> GetRolePower(int id)
        {
            var r = _db.RoleLinkPower.Where(m => m.RoleId == id).ToList();
            var p = ObjectMapper.Map<List<RoleLinkPower>, List<RoleLinkPowerDto>>(r);
            return p;
        }

        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="dto">角色信息dto</param>
        /// <returns></returns>
        public int PostRolePower(int id, string ids)
        {
            var p = _db.RoleLinkPower.Where(m => m.RoleId == id).ToList();
            if (p.Count > 0)
            {
                _db.RoleLinkPower.RemoveRange(p);
            }

            var pid = ids.Split(',');
            foreach (var item in pid)
            {
                var r = new RoleLinkPower
                {
                    RoleId = id,
                    PowerId = Convert.ToInt32(item)
                };
                _db.RoleLinkPower.Add(r);
            }
            int i = _db.SaveChanges();
            return i;
        }
    }
}
